package com.marek1and.oracle;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.SettingsFactory;
import org.hibernate.criterion.Restrictions;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class OracleJDBC {
	
	private static final String PERSISTENCE_UNIT_NAME = "OracleTest";
	  private static EntityManagerFactory factory;
	
//    private static final SessionFactory sessionFactory;
//
//    static {
//            try {
//                    Configuration configuration = new Configuration();
//                    configuration.configure();
//                    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
//                    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
//            } catch (Throwable ex) {
//                    System.err.println("Failed to create sessionFactory object." + ex);
//                    throw new ExceptionInInitializerError(ex);
//            }
//    }
//
//    public static SessionFactory getSessionFactory() {
//            return sessionFactory;
//    }

	
	public static void main(String[] args){
		
		factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
	    EntityManager em = factory.createEntityManager();
	    
	    CriteriaBuilder cb = em.getCriteriaBuilder();
	    CriteriaQuery<Book> cq = cb.createQuery(Book.class);
	    Root<Book> book = cq.from(Book.class);
	    cq.select(book);
	    TypedQuery<Book> b = em.createQuery(cq);
	    List<Book> books = b.getResultList();
	    
	    
	    // Read the existing entries and write to console
//	    Query q = em.createQuery("select t from books t");
//	    List<Book> books = q.getResultList();
	    for (Book todo : books) {
	      System.out.println(todo.getTitle());
	    }
		
//		SessionFactory sf = getSessionFactory();
//		Session session = sf.openSession();
		
//		Criteria c = session.createCriteria(Book.class);
////		c.add(Restrictions.eq("bookId", 1));

//		Query g = session.createQuery("select b from books b");
//		ArrayList<Book> b = (ArrayList<Book>)g.list();
//		for(Book book: b){
//			System.out.println(book.getTitle());
//		}
		
//		sf.close();

//		try{
//			OracleJDBConnection jdbc = OracleJDBConnection.getInstance();
//			Connection c = jdbc.getConnection();
//			
//			PreparedStatement ps = c.prepareStatement("select * from books");
//			ResultSet rs = ps.executeQuery();
//			while(rs.next()){
//				System.out.println(rs.getString("title"));
//			}
//			
//			if(rs!=null){
//				rs.close();
//			}
//			if(ps != null){
//				ps.close();
//			}
//			
//			jdbc.closeConnection();
//			
//		}catch(SQLException e){
//			System.out.println(e.getMessage());
//		}
//		
//		String path = OracleJDBC.class.getProtectionDomain().getCodeSource().getLocation().getPath();
//		System.out.println(path);
	}

}
